package xyz.mumuwei.youthcourseserver.controller;

import com.alipay.api.AlipayApiException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import xyz.mumuwei.youthcourseserver.service.OrderService;

import javax.servlet.http.HttpServletRequest;

/**
 * @author mumuwei
 * @date 2022/4/2
 */
@Slf4j
@Controller
@RequiredArgsConstructor
public class ReturnController {

    final OrderService orderService;

    /**
     * 用户主动访问url付款
     * @param orderId 用户id 和订单号 通过 & 拼接
     */
    @GetMapping("/course/pay/{orderId}")
    public String payOrderByCode(@PathVariable("orderId") String orderId, Model model) throws AlipayApiException {
        orderService.createWapAlipay(orderId, model);
        return "alipay/wap/payOrder";
    }

    /**
     * 移动端端支付成功 同步参数返回
     */
    @GetMapping("/order/wapAlipayReturn")
    public String alipayReturnWap(HttpServletRequest req, Model model) throws AlipayApiException {
        String outTradeNo = req.getParameter("out_trade_no");
        orderService.queryOrderInAlipayByCode(outTradeNo);
        model.addAttribute("orderCode", outTradeNo);
        return "alipay/return/alipayWapReturn";
    }

}
